home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / fread.c < prev    next >
C/C++ Source or Header  |  1992-03-10  |  2KB  |  77 lines

  1. /* nothing like the original from
  2.  * Dale Schumacher's dLibs
  3.  */
  4.  
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <limits.h>
  8. #include <assert.h>
  9. #include <string.h>
  10. #include "lib.h"
  11.  
  12. size_t  fread(data, size, count, fp)
  13.     register void *data;
  14.     size_t size;
  15.     size_t count;
  16.     register FILE *fp;
  17. {
  18.     register size_t n;
  19.     register long l, cnt;
  20.     register unsigned int f;
  21.  
  22.     assert((data != NULL));
  23.     assert((size != 0));
  24.     
  25.     f = fp->_flag;
  26.     if(f & _IORW) f = (fp->_flag |= _IOREAD);
  27.     if(!(f & _IOREAD) || (f & (_IOERR | _IOEOF)))
  28.         return(0);
  29.  
  30.     l = 0;
  31.     n = count * size;
  32. #if 0
  33.     if(fflush(fp))            /* re-sync file pointers */
  34.         return 0;
  35. #endif
  36.     assert((n <= (size_t)LONG_MAX));
  37.     again:    
  38.     if((cnt = fp->_cnt) > 0)
  39.     {
  40.         cnt = (cnt < n) ? cnt : n;
  41.         bcopy(fp->_ptr, data, cnt);
  42.         fp->_cnt -= cnt;
  43.         fp->_ptr += cnt;
  44.         l += cnt;
  45.         /* The cast is needed because sizeof(void) is not defined. */
  46.         /* The GCC (foolishly?) assumes 1; Sozobon complains. */
  47.         data = (char *) data + cnt;
  48.         n -= cnt;
  49.     }
  50.     /* n == how much more */
  51.     if(n > 0)
  52.     {
  53.         if(n < fp->_bsiz)
  54.         { /* read in fp->_bsiz bytes into fp->_base and do it again */
  55.         fp->_ptr = fp->_base;
  56.         if((cnt = _read(fp->_file, fp->_base, (long)fp->_bsiz)) <= 0)
  57.         {   /* EOF or error */
  58.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  59.             goto ret;
  60.         }
  61.         fp->_cnt = cnt;
  62.         goto again;
  63.         }
  64.         else
  65.         { /* read in n bytes into data */
  66.         cnt = _read(fp->_file, data, (long)n);
  67.         if (cnt <= 0) {
  68.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  69.         } else {
  70.             l += cnt;
  71.         }
  72.         }
  73.     }
  74.     ret:
  75.     return((l > 0) ? ((size_t)l / size) : 0);
  76. }
  77.